package princessWorld;

import java.util.*;
import processing.core.*;

public class MouseInteraction implements IPrincessInteraction {
	
	PApplet p;
	
	PVector mousePosition;
	float maxMouseDist;
	
	private ArrayList<IPrincessListener> princessListeners;
	
	public MouseInteraction() {
		mousePosition = new PVector();
		
		princessListeners = new ArrayList<IPrincessListener>();
	}

	public void setup(PApplet processing) {
		p = processing;
		
		//Get a factor of the distance across the the screen
		maxMouseDist = PApplet.sqrt(PApplet.pow(p.width, 2) + PApplet.pow(p.height, 2)) * 0.2f;
	}

	public void update() {
		mousePosition.set(p.mouseX, p.mouseY, 0);
		
		if(p.mousePressed) {			
			for(IPrincessListener listener: princessListeners) {
				listener.addButterfly(mousePosition);
			}
		}
	}

	public PVector getVectorAtPosition(PVector position) {
		PVector vector = new PVector(0, 0, 0);
		
		//Get the distance from the mouse and the position
		float mouseDist = mousePosition.dist(position);
		if(mouseDist < maxMouseDist) {
			//Set the vector to the position
			vector.set(position);
			
			//Subtract the mouse position to get the vector from the mouse to the position
			vector.sub(mousePosition);
			
			//Normalize the vector and multiply it with the factor between mouseDist and max Dist
			vector.normalize();
			vector.mult(mouseDist / maxMouseDist);
		}
		
		return vector;
	}

	public void addPrincessListeners(IPrincessListener listener) {
		princessListeners.add(listener);
	}
}
